package com.wenan.data
{
	import com.adobe.serialization.json.JSON;
	import com.wenan.data.ParameterCollection;
	
	import mx.collections.ArrayCollection;
	
	public class PageInfo
	{
		private var _pageName:String;
		private var _positionId:String;
		private var _positionName:String;
		private var _positionType:int = 0;
		private var _parameters: ParameterCollection;
		private var _reportIntoWay:String;
		
		public function PageInfo(pageName:String = null, positionId:String = null, 
								 positionName:String = null, positionType:int = 0, 
								 parameters: ParameterCollection = null)
		{
			this._pageName = pageName;
			this._positionId = positionId;
			this._positionName = positionName;
			this._positionType = positionType;
			this._parameters = parameters;
		}

		public function get reportIntoWay():String
		{
			return _reportIntoWay;
		}

		public function set reportIntoWay(value:String):void
		{
			_reportIntoWay = value;
		}

		public function get pageName():String
		{
			return _pageName;
		}
		
		public function set pageName(value:String):void
		{
			_pageName = value;
		}
		
		public function get positionId():String
		{
			return _positionId;
		}
		
		public function set positionId(value:String):void
		{
			_positionId = value;
		}
		
		public function get positionName():String
		{
			return _positionName;
		}
		
		public function set positionName(value:String):void
		{
			_positionName = value;
		}
		
		public function get positionType():int
		{
			return _positionType;
		}
		
		public function set positionType(value:int):void
		{
			_positionType = value;
		}
		
		public function get parameters():ParameterCollection
		{
			if (_parameters == null)
			{
				_parameters = new ParameterCollection();
			}
			return _parameters;
		}
		
		public function set parameters(value:ParameterCollection):void
		{
			_parameters = value;
		}
		
		public function createPageInfo(result:Object):void
		{
			if(!result) return;
			
			pageName = result.pageName;
			positionId = result.positionId;
			positionName = result.positionName;
			positionType = result.positionType;
			
			var parameterList:Array = result.parameters as Array;
			var parameter:Parameter;
			
			for(var i:int=0;i<parameterList.length;i++)
			{
				parameter = new Parameter(parameterList[i].name,parameterList[i].value);
				parameters.add(parameter);
			}
		}
		
		public function pageInfoToJson(type:String = null):String
		{
			var pageNameStr:String = '"pageName":"' + pageName + '"';
			var positionIdStr:String = '"positionId":"' + positionId + '"';
			var positionNameStr:String = '"positionName":"' + positionName + '"';
			var positionTypeStr:String = '"positionType":' + positionType ;
			var parameterStr:String;
			if (type == "report")
			{
				parameterStr = reportToJson(parameters);
			} else 
			{
				parameterStr = parameterToJson(parameters);
			}
			return '{' + pageNameStr + ',' + positionIdStr + ',' + positionNameStr + ',' + positionTypeStr + ',"parameters":' + parameterStr +'}';
		}
		
		private function parameterToJson(parameters:ParameterCollection):String
		{
			var json:String = "[";
			if (parameters && parameters.length > 0)
			{
				var parameter:Parameter = parameters.getAt(0);
//				json += DataUtil.objectToJson(parameter);
				json += '{"name":"' + parameter.name + '","value":"' + parameter.value + '"}'
				
				for(var i:int=1;i<parameters.length;i++)
				{
					parameter = parameters.getAt(i);
//					json += "," + DataUtil.objectToJson(parameter);
					json += ',{"name":"' + parameter.name + '","value":"' + parameter.value + '"}'
				}
			}
			
			json +="]";
			
			return json;
		}
		
		protected function reportToJson(parameters:ParameterCollection):String
		{
			var json:String = "[";
			var objArray:ArrayCollection;
			var objString:String;
			if (parameters && parameters.length > 0)
			{
				var parameter:Parameter;
				for(var i:int=0;i<parameters.length;i++)
				{
					parameter = parameters.getAt(i);
					if (parameter.name != "dataProvider" && parameter.name != "reportStyle")
					{
						if (parameter.obj)
						{
							objArray = parameter.obj as ArrayCollection;
							objString = objArrayToJson(objArray);
							json += '{"name":"' + parameter.name + '","value":"' + parameter.value + '","obj":[' + objString + ']},';
						} else 
						{
							json += '{"name":"' + parameter.name + '","value":"' + parameter.value + '"},';
						}
					}
				}
			}
			json = json.substr(0, json.length - 1);
			json +="]";
			return json;
		}
		
		protected function objArrayToJson(coll:ArrayCollection):String
		{
			var json:String = "";
			if (coll.length > 0)
			{
				for each (var obj:Object in coll)
				{
					json += JSON.encode(obj) + ",";
				}
			}
			json = json.substr(0, json.length - 1);
			return json;
		}
	}
}